<?php
include('HTGPage.class.php');
$page = new HTGPage();
/* @var $security HTSecurity */
$security = $page->mySecurity;

$isPayingMember=false;
if($security->isLoggedIn()) {
	$isPayingMember = $page->myUtils->isPaidMember($_SESSION['u_id']);
}

$leagueId = 2; //nfl league EventType

$picks = $page->myUtils->getAllPicksByLeagueId($leagueId);

$headlines = $page->myUtils->getBlogEntries($leagueId);

$firstHeadlineDate = '';
if(count($headlines) > 0) {
	$firstHeadlineDate = @strtotime($headlines[0]['be_initdate']);
}

@error_log(__FILE__ . ' ' . __LINE__ . " picks: " . array_count_values($picks));

if(!(@array_count_values($picks) > 0)) {
	//TODO automatically create issues and produce the tracking id to it if the users decides to pursue it.
	$page->content .= '<span class="error">We seem to be having some issues finding our picks for ya.  An alert has been sent and we\'re currently looking into it.</span>';
	$page->display();
	die();
}

//****BROWSERS OUTPUT STARTED HERE *****
$page->displayHeader();

//echo '<span class="pageTitle">NFL Picks for: ' . @date('l F jS') . '</span><br />';
echo '<br />';
$lastEventId=false;
$lastPickDate=false;

$headlineNum=0;
//loop through the picks
foreach($picks as $pick) {
	$pickIsPublic = false;
	if( @$pick['p_publicdate'] == '0000-00-00 00:00:00' ||  @mktime()  > @strtotime(@$pick['p_publicdate'])) {
		$pickIsPublic=true;
	}
	$canSeePick=false;

	$pickDate = @strtotime($pick['p_initdate']);
	if(1 == 1) {


		$eventId = $pick['e_id'];
		$pickDate = $pick['p_initdate'];
		if($lastEventId == false || $lastEventId != $eventId) {
			//It's the next post

			if($lastEventId){
				//close the previous post
				?>
</tbody>
</table>
</td>
</tr>
</table>
</div>
<div class="meta">
	<p class="credit">
		Posted by <a href="about.php"> TheGenius</a> @
		<?= $lastPickDate ?>
	</p>

	<p class="links">
		<a href="#" class="comments">Comments (0)</a>
		&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="#" class="more">Read more</a>
	</p>
</div>
</div>


<?php
while($firstHeadlineDate > @strtotime($pickDate) && (!(empty($firstHeadlineDate)))) {
	//display this Headline
	$headline = $headlines[$headlineNum];
	$text = $headline['be_text'];
	$title = $headline['be_title'];
	echo '<div class="headlineEntryDiv"><div class="post" style="width: 583px;">
<div class="title">
<h2><a href="#">' . $title . '</a></h2>
<p>' . @$wrongvarheadline['be_initdate']  . '</p>
</div>
<div class="entry">
<p>' . stripslashes($text) . '</p></div><br />';


	$headlineNum++;
	if(@$headlines[$headlineNum]) {
		$firstHeadlineDate = strtotime($headlines[$headlineNum]['be_initdate']);
	} else {
		$firstHeadlineDate = 0;
	}

}



			}
			?>


<div class="post" style="width: 583px;">
	<div class="title">
		<h2>
			<a href="#"><?php echo $pick['e_vis_team']; ?> @ <?= $pick['e_home_team'] ?>
			<?= $pick['p_pt_id'] == 2 ? ' @ 1/2' : ''	?> </a>
		</h2>
		<p>
		<?= @$wrongvarpick['e_starttime'] ?>
		</p>

	</div>
	<div class="entry">
		<p>
		<?= $isPayingMember || $pickIsPublic ? $pick['p_text'] : '<a href="getit.php?r=ncaaf.php"><img src="images/buy_now.png"/></a>' ?>
		</p>
	</div>
	<div class="pickTableDiv">
		<table class="pickTableContainer">
			<tr>
				<td class="pickTableIconContainer"><img src="images/<?= $pick['p_pt_id'] == 2 ? 'ref_transparent.png' : 'thumbs_up.png'	?>" />
				</td>
				<td>
					<table class="pickTable">
						<thead>
							<tr>
								<th></th>

								<th>Genius Pick</th>
								<th>Teams</th>
								<th>League</th>
								<th>Starts</th>
							</tr>
						</thead>
						<tfoot>

						</tfoot>
						<tbody>
							<tr>
								<td></td>
								<td><span class="pickTablePic"><?= $isPayingMember  || $pickIsPublic  ? $pick['p_pick'] : '<a href="getit.php?r=ncaaf.php"><img src="images/buy_now.png"/></a>' ?>
								</span></td>
								<td><?= $pick['e_vis_team'] ?> @ <?= $pick['e_home_team'] ?></td>
								<td>NCAAF</td>

								<td style="white-space: nowrap;"><?= $pick['e_starttime'] ?></td>
							</tr>
							
							
							
							

<?php 
	} else { //It's a related post
?>
<tr>
<td>@ 1/2</td>
<td><span class="pickTablePic"><?= $isPayingMember ? $pick['p_pick'] : '<a href="getit.php?r=ncaaf.php"><img src="images/buy_now.png"/></a>' ?></span></td>
<td></td>
<td></td>
<td></td>
</tr>
<?php 	
	} 
	$lastEventId = $eventId;
	$lastPickDate = $pickDate;
  }
}
?>
</tbody>
					</table>
				</td>
			</tr>
		</table>
	</div>
	<div class="meta">
		<p class="credit">
			Posted by <a href="about.php">TheGenius</a> @
			<?= $lastPickDate ?>
			PDT
		</p>

		<p class="links">
			<a href="#" class="comments">Comments (0)</a>
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="#" class="more">Read more</a>
		</p>
	</div>
</div>


<?php 
$page->displayFooter();
?>